package com.zksk.admin.controller.portal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.domain.portal.vo.PolicyInfoVo;
import com.zksk.admin.service.portal.PolicyInfoService;
import com.zksk.common.core.constant.PolicyInfoConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.PolicyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 政策资讯
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/portal/policy")
@Api(value = "PolicyInfoController", tags = "政策资讯相关接口")
public class PolicyInfoController extends BaseController {

    @Autowired
    private PolicyInfoService policyInfoService;



    @Log(title = "获取政策资讯分页列表",businessType = BusinessType.SELECT)
    @PostMapping("/list")
    @ApiOperation(value = "获取政策资讯分页列表")
    public R<Page<PolicyInfo>> list(@RequestBody PolicyInfoVo policyInfoVo){
        //构建分页
        Page<PolicyInfo> pageInfo=new Page<>(policyInfoVo.getPageNum(), policyInfoVo.getPageSize());

        //构建条件
        LambdaQueryWrapper<PolicyInfo> wrapper = new LambdaQueryWrapper<PolicyInfo>()
                .eq(StringUtils.isNotBlank(policyInfoVo.getType()), PolicyInfo::getType, policyInfoVo.getType())
                .like(StringUtils.isNotBlank(policyInfoVo.getTitleName()), PolicyInfo::getTitleName, policyInfoVo.getTitleName())
                .orderByDesc(PolicyInfo::getInfoTime);

        Page<PolicyInfo> page = policyInfoService.page(pageInfo, wrapper);

        return R.ok(page);

    }

    /**
     * 根据政策咨询id查询资讯信息
     * @param id
     * @return
     */
    @Log(title = "根据政策资讯id查询资讯信息",businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据政策咨询id查询资讯信息")
    public R<PolicyInfo> select(@PathVariable Long id) {
        PolicyInfo policyInfo = policyInfoService.getById(id);
        if (policyInfo.getLookNum()==null){
            policyInfo.setLookNum(PolicyInfoConstants.LOOK_NUM_1);
        }else {
            policyInfo.setLookNum(policyInfo.getLookNum()+1);
        }
        policyInfoService.updateById(policyInfo);
        return R.ok(policyInfo);
    }

    /**
     * 删除资讯信息
     * @param ids
     * @return
     */
    @Log(title = "删除资讯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    @ApiOperation(value = "删除奖惩信息(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        policyInfoService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }


    /**
     * 新增资讯信息
     * @param policyInfo
     * @return
     */
    @Log(title = "新增资讯信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增资讯信息")
    public R<PolicyInfo> add(@Validated(Insert.class) @RequestBody PolicyInfo policyInfo) {
        policyInfo.setCreateBy(SecurityUtils.getUsername());
        policyInfoService.save(policyInfo);
        return R.ok(policyInfo,"新增成功");
    }

    /**
     * 更新资讯信息
     * @param policyInfo
     * @return
     */
    @Log(title = "更新资讯信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新资讯信息")
    public R<PolicyInfo> update(@Validated(Update.class) @RequestBody PolicyInfo policyInfo) {
        policyInfo.setUpdateBy(SecurityUtils.getUsername());
        policyInfoService.updateById(policyInfo);
        return R.ok(policyInfo,"保存成功");
    }


}
